<?php

class Muser extends CI_Model {
	
	public $table = 'tbl_users';
	public $pri_key="user_id";


	function __construct()

	{

		parent::__construct();

	}
	public function del_item($id)
	{
		$this->db->where($this->pri_key, $id);
		$this->db->delete($this->table); 
			
	}
	
	public function get_per()
	{
		return $this->db->get("permission")->result_array() ;
		//print_r($this->db->get("permission")->result_array());
	}
	
	public function insert($data){

		if($this->db->insert($this->table,$data))
			return true;
		
		return false;

	}
		public function update($data,$id)
	{
		//$this->db->where[ar_where] = "";
		$where = $this->pri_key." =".$id;
		if($this->db->update($this->table,$data,$where))
			return true;
		
		return false;
	
	}
	
	public function info_form_id($id)
	{
		if(is_numeric($id) && $id !="")
		{
		$this->db->where($this->pri_key.' =',$id);
		return $this->db->get($this->table)->result_array();
		}
		else
			return false;
	}
	public function check_email($email)
	{
		$this->db->where('user_email =',$email);
		if($this->db->count_all($this->table) >0)
			return true; // email nay da ton tai va ko su dung duoc
		return false; // co the su dung email nay
			
 	}
	public function list_data($num,$off)
	{
		$this->db->limit($off,$num);
		$this->db->where('user_level !=',2);
		if($this->db->count_all($this->table) >0)
			//return $this->db->get($this->table)->result_array();
			{
			//	$this->db->join('permission', 'per_id = user_lvl');
	
				return $this->db->get($this->table)->result_array();
			}
		return false;
	}
	// dem so record 
	function count_all()
	{
		return $this->db->count_all_results($this->table);
	}
	
		public function check_username($username,$id='')
	{
		$sql="select * from $this->table where user_name = '".$username."'";
		//$this->db->where('user_email =',$email);
		if($id != '')
		{
			//$this->db->where("user_id !=",$id);
			$sql= $sql." and user_id != ".$id;
			
		}
		$q= $this->db->query($sql);
	
		if($q->num_rows() >0)
			return false; //co the su dung email nay 
		return true; // email nay da ton tai va ko su dung duoc
			
 	}
	//***** check Login ****//
	public function check_user($email,$password)
	{
		$where=array("user_name"=>$email,"user_pass"=>pass_md5($password));
	//	$this->db->where('user_email',$email); 
		//$this->db->where('user_pass',md5($password)); 
		
		$row=$this->db->get_where($this->table,$where)->result_array();
		
	
		if($this->db->count_all_results() == 1)
		{
			return $row; 
		}
		return false;
	
	}


	function delete()

	{
		//print_r($_POST);
		if (isset($_POST['checkbox1']))

		{
			
			foreach($_POST['checkbox1'] as $id) {

		

				//xoa CSDL

		
				$this->db->where($this->pri_key, $id);
				$this->db->delete($this->table); 
		

			}			

		}

	}




}//end model class





?>